<?php

namespace Library\Support;


use Exception;
use Library\QueryBuilder;

class MQ
{

    protected string $sql = "";
    protected string $tableName = "";
    protected string $alias = "";

    protected array $where = [];
    protected array $fields = [];
    protected array $joins = [];

    protected QueryBuilder $builder;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        $database = app()->getConfig("database");
        $config = [
            'hostname'=>$database['host'],
            'username'=>$database['user'],
            'password'=>$database['password'],
            'database'=>$database['dbname'],
            'port'=>$database['port'],
            'socket'=>$database['socket']??null,
        ];
        $this->builder = new QueryBuilder($config);
    }

    /**
     * @return QueryBuilder
     */
    public static function build(): QueryBuilder
    {
        $self = new static();
        return $self->builder;
    }

    /**
     * @param string $tableName
     * @param string $alias
     * @return $this
     */
    public static function table(string $tableName, string $alias = ""): static
    {
        $self = new static();
        return $self->from($tableName, $alias);
    }

    /**
     * @param string $tableName
     * @param string $alias
     * @return $this
     */
    protected function from(string $tableName, string $alias = ""): static
    {
        $this->tableName = $tableName;
        $this->alias = $alias;
        return $this;
    }

    public function fields(array $fields){
        return $this;
    }




    public function select( ){

        return $this;
    }

    public function insert(){

    }

    public function update(){

    }

    public function count(){

    }

    public function avg(){

    }

    /**
     * @return string
     */
    public function toSql(): string
    {
        return $this->sql;
    }

    /**
     * @return string
     */
    public function toArray(): string
    {
        return $this->builder->fetch_array(MYSQLI_ASSOC);
    }
}